

package com.chinaoly.sdk.base;

import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.chinaoly.sdk.presenter.lifecycle.LifeCycleWrapper;


/**
 * titleTbar 未做实现,可以继承 ,实现自定义toolbar
 */
public abstract class TitleNotImplActivity<Binding extends ViewDataBinding> extends ChinaolyActivity<Binding> {



    /**
     * root view
     */
    private LinearLayout parentLinearLayout;


    /**
     * baseactivity布局
     *
     * @param layoutResID
     */
    private void initContentView(@LayoutRes int layoutResID) {
        if (!showToolBar()) {
            mBinding = DataBindingUtil.setContentView(mActivity, getLayoutId());
            return;
        }
        //viewGroup 去除
        ViewGroup viewGroup = findViewById(android.R.id.content);
        viewGroup.setId(View.NO_ID);
        parentLinearLayout = new LinearLayout(this);
        parentLinearLayout.setOrientation(LinearLayout.VERTICAL);
        parentLinearLayout.setId(android.R.id.content);
        viewGroup.removeAllViews();
        viewGroup.addView(parentLinearLayout);
        View v = View.inflate(mActivity, layoutResID, null);
        mBinding = DataBindingUtil.setContentView(mActivity, getLayoutId());
        View root = mBinding.getRoot();
        final ViewGroup parent = (ViewGroup) root.getParent();
        if (parent != null) {
            parent.removeView(root);
        }
        parentLinearLayout.addView(v);
        parentLinearLayout.addView(root);
        getLifecycle().addObserver(new LifeCycleWrapper() {
            @Override
            public void onDestroy(LifecycleOwner owner) {
                parentLinearLayout.removeAllViews();
            }
        });

        bindTitleViewId(v);

    }

    protected void bindTitleViewId(View view) {
        throw  new RuntimeException("bindTitleViewId not impl");
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initContentView(getTitleLayoutId());
        initData();
    }

    protected int getTitleLayoutId() {
       throw  new RuntimeException(" getTitleLayoutId not impl");
    }


    protected abstract void initData();

    protected abstract int getLayoutId();




    /**
     * startActivity
     *
     * @param clazz
     */
    public void readyGo(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    /**
     * startActivity with bundle
     *
     * @param clazz
     * @param bundle
     */
    public void readyGo(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * startActivity then finish
     *
     * @param clazz
     */
    public void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    /**
     * startActivity with bundle then finish
     *
     * @param clazz
     * @param bundle
     */
    public void readyGoThenKill(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        finish();
    }

    /**
     * startActivityForResult
     *
     * @param clazz
     * @param requestCode
     */
    public void readyGoForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        startActivityForResult(intent, requestCode);
    }

    /**
     * startActivityForResult with bundle
     *
     * @param clazz
     * @param requestCode
     * @param bundle
     */
    public void readyGoForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 点击空白区域隐藏键盘.
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    private boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    protected boolean showToolBar() {
        return true;
    }

    protected void setTitleInfo(String title) {

    }

    protected void setLeftCallBack(View.OnClickListener listener) {

    }

    /**
     * 布局中的 viewmodel 的name 统一 为 item
     * @param viewModel
     */
    protected final void setViewModel(AndroidViewModel viewModel){
        mBinding.setVariable(com.chinaoly.sdk.BR.item,viewModel);
    }



}
